//
// Created by hkkzzxz on 24-9-1.
//

#ifndef YUNSHI_REDISCONNECTIONPOOL_H
#define YUNSHI_REDISCONNECTIONPOOL_H

// Redis连接池

#include <iostream>
#include <memory>
#include <hiredis/hiredis.h>
#include <queue>
#include <condition_variable>

#include "Singleton.h"
#include "LogManager.h"

class RedisConnectionPool : public Singleton<RedisConnectionPool> {
    friend class Singleton<RedisConnectionPool>;

public:
    // 获取连接
    std::shared_ptr<redisContext> getConnection();

    // 释放连接回连接池
    void releaseConnection(std::shared_ptr<redisContext> conn);

private:
    RedisConnectionPool();

    ~RedisConnectionPool();

    // 最大连接数量
    const int maxPoolSize = 3;

    const char *const RedisAddress = "127.0.0.1";
    const int port = 6379;

    // 创建一个新的Redis连接
    std::shared_ptr<redisContext> createConnection();

    // 存储Redis连接的队列
    std::queue<std::shared_ptr<redisContext>> pool_;
    // 同步访问的互斥锁
    std::mutex mutex_;
    // 用于线程间同步的条件变量
    std::condition_variable cv_;

    LogManager *logManager = LogManager::getInstance();
    std::shared_ptr<spdlog::logger> consoleLogger = logManager->getConsoleLogger();
    std::shared_ptr<spdlog::logger> fileLogger = logManager->getFileLogger();
};


#endif //YUNSHI_REDISCONNECTIONPOOL_H
